package org.androidpush.utils;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;

import org.androidpush.R;
import org.androidpush.client.Constants;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Date;

/**
 * Created by huang on 2017/3/31 0031.
 */
public class ConstantUtils {

    public static final String STATUS = "status";

    public static final String USER_ID = "userId";
    public static final String PASSWORD = "password";

    public static final String NET_ERROR = "NET_ERROR";

    public static final String STUDENT = "STUDENT";
    public static final String TEACHER = "TEACHER";
    public static final String ADMIN = "ADMIN";

    public static final String STU_ID = "STUID";
    public static final String TEA_ID = "TEAID";
    public static final String ADM_ID = "ADMID";
    public static final String PRO_ID = "PROID";
    public static final String TEA_COU_ID = "TEACOUID";
    public static final String MAJ_ID = "MAJID";

    public static final String FRIEND_ID = "FRIENDID";
    public static final String FRIEND_NAME = "FRIENDNAME";

    public static final String MESS_TITLE = "MESSTITLE";
    public static final String MESS_CONTENT = "MESSCONTENT";
    public static final String MESS_DATE = "MESSDATE";
    public static final String MESS_FROM = "MESSFROM";

    public static final String SUCCESS = "SUCCESS";
    public static final String FAILUER = "FAILURE";
    public static final String CREDIT_ERROR = "CREDIT_ERROR";

    public static String IP = "192.168.3.22";

    public static String URL = "http://" + IP + ":8080/";

    /**
     * Println Toast
     *
     * @param logString String
     * @param activity  Activity
     */
    public static void logToast(Activity activity, String logString) {
        Toast toast = toast = Toast.makeText(activity, logString, Toast.LENGTH_SHORT);
        toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
        toast.show();
    }

    public static void aetActionBar(Context context, String name) {
        final Activity activity = (Activity) context;
        ActionBar actionBar = activity.getActionBar();
        actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
        actionBar.setCustomView(R.layout.xml_actionbar);
        TextView headerText = (TextView) activity.findViewById(R.id.id_actionbar_name_tev);
        ImageView backImg = (ImageView) activity.findViewById(R.id.id_actionbar_back_img);
        headerText.setText(name);
        backImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                activity.finish();
                activity.overridePendingTransition(R.anim.slide_right_out,
                        R.anim.slide_right_in);
            }
        });
    }

    public static <T> T parseJsonWithGson(String jsonData, Class<T> type) {
        Gson gson = new Gson();
        T result = gson.fromJson(jsonData, type);
        return result;
    }

    public static <T> List<T> parseJsonArrayWithGson(String jsonData, Class<T> type) {
        ArrayList<T> arrayList = null;
        if(jsonData!=null) {
            JsonParser parser = new JsonParser();

            JsonArray jsonArray = parser.parse(jsonData).getAsJsonArray();
            Gson gson = new Gson();
            arrayList = new ArrayList<>();
            for (JsonElement jsonBean : jsonArray) {
                T bean = gson.fromJson(jsonBean, type);
                arrayList.add(bean);
            }
        }
        return arrayList;
    }

    public static String parseGsonWithJson(Object object){
        Gson gson = new Gson();
        return gson.toJson(object);
    }

    public static String getDateFrom(final String url) {
        String result = null;
        OkHttpClient okHttpClient = new OkHttpClient();
        Request request = new Request.Builder().url(url).build();
        try {
            Response response = okHttpClient.newCall(request).execute();
            if (response.isSuccessful()) {
                result = (String) response.body().string();
            }else {
                result = ConstantUtils.NET_ERROR;
            }
        } catch (IOException e) {
            e.printStackTrace();
            result = ConstantUtils.NET_ERROR;
        }
        return result;
    }

    public static String getStatusStrWithId(String str){
        if(ConstantUtils.STUDENT.equals(str)){
            return ConstantUtils.STU_ID;
        }else if(ConstantUtils.TEACHER.equals(str)){
            return ConstantUtils.TEA_ID;
        }else {
            return ConstantUtils.ADM_ID;
        }
    }

    public static SharedPreferences getSharedPreferences(Activity activity){
        SharedPreferences sp = activity.getSharedPreferences(Constants.SHARED_PREFERENCE_NAME, Context.MODE_APPEND);
        return sp;
    }

    public static int getScheduleTextViewBG(){
        int[] bg = {R.color.ivory,R.color.pink,R.color.orange,R.color.tomato,R.color.salmon,R.color.khaki,R.color.lightcoral,
                R.color.violet,R.color.peru,R.color.darkgoldenrod,R.color.powderblue,R.color.greenyellow,R.color.sienna,
        R.color.gray,R.color.lawngreen,R.color.white,R.color.divider};
        int bgcolor = (int) (Math.random()*bg.length);
        return bg[bgcolor];
    }

    public static String getFormatDate() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        return format.format(new Date());
    }

    public static String getUserId(Activity activity){
        SharedPreferences sharedPreferences = activity.getSharedPreferences(Constants.SHARED_PREFERENCE_NAME, Context.MODE_APPEND);
        return sharedPreferences.getString(ConstantUtils.USER_ID, "");
    }

    /**
     * 判断是否有网络
     *
     * @return
     */
    public static boolean isNetworkConnected(Activity activity) {
        ConnectivityManager connManager = (ConnectivityManager) activity.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connManager.getActiveNetworkInfo() != null) {
            return connManager.getActiveNetworkInfo().isAvailable();
        }
        return false;
    }
}
